/**
 * 
 */
package com.yidu.ssm.service.impl;

import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import com.yidu.common.Tools;
import com.yidu.ssm.action.FlowerVo;
import com.yidu.ssm.dao.FlowerMapper;
import com.yidu.ssm.domain.Flower;
import com.yidu.ssm.service.FlowerService;

/**
 * 鲜花服务的实现类
 * @author liandyao
 * @date 2018年7月5日
 * @version 1.0
 */
@Service
public class FlowerServiceImpl implements FlowerService{
	
	@Resource
	private FlowerMapper flowerMapper;

	@Override
	public int deleteByPrimaryKey(Integer flId) {
		
		return flowerMapper.deleteByPrimaryKey(flId);
	}

	@Override
	public int insert(Flower record) {
		 
		return flowerMapper.insert(record);
	}

	@Override
	public int insertSelective(Flower record) {
		 
		return flowerMapper.insertSelective(record);
	}

	@Override
	public FlowerVo selectByPrimaryKey(Integer flId) {
		
		Flower flower = flowerMapper.selectByPrimaryKey(flId);
		
		FlowerVo vo = new FlowerVo();
		
		BeanUtils.copyProperties(flower, vo);
		//日期类型的字符串
		vo.setFlDateStr(DateFormatUtils.format(flower.getFlDate(),"yyyy-MM-dd"));
		
		return vo ;
	}

	@Override
	public int updateByPrimaryKeySelective(Flower record) {
		 
		return flowerMapper.updateByPrimaryKeySelective(record);
	}

	@Override
	public int updateByPrimaryKey(Flower record) {
		 
		return flowerMapper.updateByPrimaryKey(record);
	}

	@Override
	public List<Flower> selectAll(FlowerVo vo) {
		
		return flowerMapper.selectAll(vo);
	}

	@Override
	public int selectCount(FlowerVo vo) {
		
		return flowerMapper.selectCount(vo);
	}

	@Override
	public int addOrUpdate(FlowerVo vo) {
		Flower fl = new Flower();
		BeanUtils.copyProperties(vo, fl);
		//如果copy不了的属性,则自己手动增加
		Date date = null ;
		try {
			date = DateUtils.parseDate(vo.getFlDateStr(), "yyyy-MM-dd");
		} catch (ParseException e) { 
			e.printStackTrace();
		}
		
		fl.setFlDate(date);
		int rows = 0;
		if(vo.getFlId()!=null){
			
			rows = updateByPrimaryKeySelective(fl) ;
		}else{
			rows = insertSelective(fl);
		}
		
		return rows;
	}

}
